<?php

require "../utility.php";

function updateCartItemToDatabase(mysqli $conn, string $user_id, string $book_id, string $cnt): bool {
    $update_str = "UPDATE Cart SET cnt = ? WHERE userID = ? AND bookID = ?;";
    $update_stmt = $conn->prepare($update_str);
    $update_stmt->bind_param("iii", $cnt, $user_id, $book_id);
    $status = $update_stmt->execute();
    return $status;
}

$conn = connect_mysql();
$user_id = getUserIdBySessionId($conn);

http_response_code(403);

if ($user_id !== null &&
    check_keys($_GET, "bookID", "count")) {
    
    $book_id = $_GET["bookID"];
    $cnt     = $_GET["count"];
    if (updateCartItemToDatabase($conn, $user_id, $book_id, $cnt)) {
        // success
        http_response_code(200);
    }
}

$conn->close();

?>